﻿using Core;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using System.Data.SqlClient;
using System.Data;

namespace DAL
{
    public class TeachingMaterialDAL : BaseDAL
    {
        #region 教材表导入
        /// <summary>
        /// 教材表导入
        /// </summary>
        /// <returns></returns>
        public bool GetTeachingMaterial(int id)
        {
            using (WebClient wc = new WebClient())
            {
                wc.Encoding = System.Text.Encoding.GetEncoding("utf-8");
                string strTm = string.Empty;
                try
                {
                    strTm = wc.DownloadString(ConfigHelper.GetConfigString("RemoteUrl") + "/Ayyfford/GetTeachingMaterial/"+id);
                }
                finally
                {
                    wc.Dispose();
                }
                if (!string.IsNullOrWhiteSpace(strTm))
                {
                    var lTm = JsonConvert.DeserializeObject<List<Model.TeachingMaterial>>(strTm);

                    #region 数据入库
                    using (var conn = new SqlConnection(connection))
                    {
                        conn.Open();
                        IDbTransaction transaction = conn.BeginTransaction();
                        try
                        {
                            lTm.ForEach(tm =>
                            {
                                conn.Execute("if not exists (select ID from TeachingMaterial where ID=" + tm.ID + @")  
                                                begin 
                                                    insert into TeachingMaterial(ID,SubjectCode,GradeCode,Type,Version,SubVersion,UpOrDown,SchoolID) values (@ID,@SubjectCode,@GradeCode,@Type,@Version,@SubVersion,@UpOrDown,@SchoolID)
                                                end
                                                    else
                                                begin
                                                    update TeachingMaterial set SubjectCode=@SubjectCode,GradeCode=@GradeCode,Type=@Type,Version=@Version,SubVersion=@SubVersion,UpOrDown=@UpOrDown,SchoolID=@SchoolID where ID=@ID
                                                end", tm, transaction);
                            });
                            transaction.Commit();

                            #region 教材绑定表导入
                            lTm.ForEach(tm =>
                            {
                                GetTeachingMaterialBind(tm.ID);
                            });
                            #endregion

                            return true;
                        }
                        catch (Exception ex)
                        {
                            transaction.Rollback();
                            Log.errorlog(ex);
                        }
                        conn.Close();
                    }
                    #endregion

                }
            }
            return false;
        }


        #endregion

        #region 教材绑定表导入
        /// <summary>
        /// 教材绑定表导入
        /// </summary>
        /// <returns></returns>
        bool GetTeachingMaterialBind(int id)
        {
            using (WebClient wc = new WebClient())
            {
                wc.Encoding = System.Text.Encoding.GetEncoding("utf-8");
                string strTmb = string.Empty;
                try
                {
                    string xx = ConfigHelper.GetConfigString("RemoteUrl") + "/Ayyfford/GetTeachingMaterialBind/" + id + "?sid=" + ConfigHelper.GetConfigString("SchoolID");
                    strTmb = wc.DownloadString(ConfigHelper.GetConfigString("RemoteUrl") + "/Ayyfford/GetTeachingMaterialBind/" + id + "?sid=" + ConfigHelper.GetConfigString("SchoolID"));
                }
                finally
                {
                    wc.Dispose();
                }
                if (!string.IsNullOrWhiteSpace(strTmb))
                {
                    var lTmb = JsonConvert.DeserializeObject<List<Model.TeachingMaterialBind>>(strTmb);

                    #region 数据入库
                    using (var conn = new SqlConnection(connection))
                    {
                        conn.Open();
                        IDbTransaction transaction = conn.BeginTransaction();
                        try
                        {
                            lTmb.ForEach(tmb =>
                            {
                                conn.Execute("if not exists (select ID from TeachingMaterialBind where ID=" + tmb.ID + @")  
                                                begin 
                                                    insert into TeachingMaterialBind(ID,SchoolID,GradeCode,MaterialID,SubjectCode,GradeInfoId) values (@ID,@SchoolID,@GradeCode,@MaterialID,@SubjectCode,@GradeInfoId)
                                                end
                                                    else
                                                begin
                                                    update TeachingMaterialBind set SchoolID=@SchoolID,GradeCode=@GradeCode,MaterialID=@MaterialID,SubjectCode=@SubjectCode,GradeInfoId=@GradeInfoId where ID=@ID
                                                end", tmb, transaction);
                            });
                            transaction.Commit();
                            return true;
                        }
                        catch (Exception ex)
                        {
                            transaction.Rollback();
                            Log.errorlog(ex);
                        }
                        conn.Close();
                    }
                    #endregion
                }
            }
            return false;
        }
        #endregion
    }
}
